package org.weasel.data;

import java.io.File;
import java.util.ArrayList;

import org.jdom.Element;
import org.weasel.business.Company;
import org.weasel.business.Developer;
import org.weasel.business.Iteration;
import org.weasel.business.Project;
import org.weasel.business.Release;
import org.weasel.business.Task;
import org.weasel.business.Unscheduled;
import org.weasel.business.UserStory;

public class XMLSaver {
	private XMLManager xmlMan; 
	
	public XMLSaver(){
		
	}
	
	/**
	 * Saves a Company object (which contains projects, etc...) to the XML file
	 * specified by path
	 * @param path = location of the XML file where to save the data
	 * @param company = object of type Company
	 * @throws Exception
	 */
	public void save(String path, Company company) throws Exception{
		File f = new File(path);
		if (f.exists())
			f.delete();
		
		xmlMan = new XMLManager(path);
		xmlMan.createRootElement("root");
		Element companyNameEl = new Element("companyname");
		companyNameEl.setText(company.getName());
		
		Element developersEl = new Element("developers");
		ArrayList<Developer> developers = company.getDevelopers();
		for (Developer developer : developers) {
			Element developerEl = new Element("developer");
			Element devIdEl = new Element("id");
			devIdEl.setText(developer.getId());
			Element nameEl = new Element("name");
			nameEl.setText(developer.getName());
			Element emailEl = new Element("email");
			emailEl.setText(developer.getEmail());
			Element posEl = new Element("position");
			posEl.setText(developer.getPosition());
			developerEl.addContent(devIdEl);
			developerEl.addContent(nameEl);
			developerEl.addContent(emailEl);
			developerEl.addContent(posEl);
			
			developersEl.addContent(developerEl);
		}
		
		Element projectsEl = new Element("projects");
		ArrayList<Project> projects = company.getProjects();
		for (Project project : projects) {
			projectsEl.addContent(createProjectElement(project));
		}
		
		xmlMan.addElement(xmlMan.getRootElement(), companyNameEl);
		xmlMan.addElement(xmlMan.getRootElement(), developersEl);
		xmlMan.addElement(xmlMan.getRootElement(), projectsEl);
		
		xmlMan.writeToFile();
	}
	
	/**
	 * Utility method to create the proper JDom Element structure out of the
	 * object passed as parameter
	 * @param project
	 * @return
	 */
	private Element createProjectElement(Project project){
		//		add basic elements
		Element projectEl = new Element("project");
		Element projectIdEl = new Element("id");
		projectIdEl.setText(project.getId());
		Element projectNameEl = new Element("name");
		projectNameEl.setText(project.getName());
		Element projectNotesEl = new Element("notes");
		projectNotesEl.setText(project.getNotes());
		Element projectStartDateEl = new Element("startDate");
		projectStartDateEl.setText(project.getStartDate());
		Element projectIterationDaysEl = new Element("iterationDays");
		projectIterationDaysEl.setText(String.valueOf(project.getIterationDays()));
		
		Element projectDevelopersEl = new Element("developers");
		ArrayList<Developer> developers = project.getDevelopers();
		for (Developer developer : developers) {
			projectDevelopersEl.addContent(createDeveloperEl(developer));
		}
		
		Element projectReleaseEl = new Element("releases");
		ArrayList<Release> releases = project.getReleases();
		for (Release release : releases) {
			projectReleaseEl.addContent(createReleaseElement(release));
		}
		
		
		projectEl.addContent(projectIdEl);
		projectEl.addContent(projectNameEl);
		projectEl.addContent(projectNotesEl);
		projectEl.addContent(projectStartDateEl);
		projectEl.addContent(projectIterationDaysEl);
		projectEl.addContent(projectDevelopersEl);
		projectEl.addContent(projectReleaseEl);
		projectEl.addContent(createUnscheduledEl(project.getUnscheduled()));
		
		return projectEl;
	}

	/**
	 * Utility method to create the proper JDom Element structure out of the
	 * object passed as parameter
	 * @param release
	 * @return
	 */
	private Element createReleaseElement(Release release) {
		Element releaseEl = new Element("release");
		Element idEl = new Element("id");
		idEl.setText(release.getId());
		Element nameEl = new Element("name");
		nameEl.setText(release.getName());
		Element releaseNotesEl = new Element("notes");
		releaseNotesEl.setText(release.getNotes());
		
		Element iterationsEl = new Element("iterations");
		ArrayList<Iteration> iterations = release.getIterations();
		for (Iteration iteration : iterations) {
			iterationsEl.addContent(createIterationElement(iteration));
		}
		
		releaseEl.addContent(idEl);
		releaseEl.addContent(nameEl);
		releaseEl.addContent(releaseNotesEl);
		releaseEl.addContent(iterationsEl);
		releaseEl.addContent(createUnscheduledEl(release.getUnscheduled()));
		
		return releaseEl;
	}

	/**
	 * Utility method to create the proper JDom Element structure out of the
	 * object passed as parameter
	 * @param unscheduled
	 * @return
	 */
	private Element createUnscheduledEl(Unscheduled unscheduled) {
		
		Element unscheduledEl = new Element("unscheduled");
		Element idEl = new Element("id");
		idEl.setText(unscheduled.getId());
		Element userstoriesEl = new Element("userstories");
		ArrayList<UserStory> userstories = unscheduled.getUserStories();
		for (UserStory story : userstories) {
			userstoriesEl.addContent(createUserStoryElement(story));
		}
		
		unscheduledEl.addContent(idEl);
		unscheduledEl.addContent(userstoriesEl);
		return unscheduledEl;
	}

	/**
	 * Utility method to create the proper JDom Element structure out of the
	 * object passed as parameter
	 * @param iteration
	 * @return
	 */
	private Element createIterationElement(Iteration iteration) {
		Element iterationEl = new Element("iteration");
		Element idEl = new Element("id");
		idEl.setText(iteration.getId());
		Element nameEl = new Element("name");
		nameEl.setText(iteration.getName());
		Element iterationNotesEl = new Element("notes");
		iterationNotesEl.setText(iteration.getNotes());
		
		Element userstoriesEl = new Element("userstories");
		ArrayList<UserStory> userstories = iteration.getUserStories();
		for (UserStory story : userstories) {
			userstoriesEl.addContent(createUserStoryElement(story));
		}
		
		iterationEl.addContent(idEl);
		iterationEl.addContent(nameEl);
		iterationEl.addContent(iterationNotesEl);
		iterationEl.addContent(userstoriesEl);
		
		return iterationEl;
	}

	/**
	 * Utility method to create the proper JDom Element structure out of the
	 * object passed as parameter
	 * @param story
	 * @return
	 */
	private Element createUserStoryElement(UserStory story) {
		Element userStoryEl = new Element("userstory");
		Element idEl = new Element("id");
		idEl.setText(story.getId());
		
		Element nameEl = new Element("name");
		nameEl.setText(story.getName());
		
		Element generatedDateEl = new Element("generatedDate");
		generatedDateEl.setText(story.getGeneratedDate());
		
		Element finishedDateEl = new Element("finishedDate");
		finishedDateEl.setText(story.getFinishedDate());
		
		Element descrEl = new Element("description");
		descrEl.setText(story.getDescription());
		
		Element businessEl = new Element("businessValue");
		businessEl.setText(String.valueOf(story.getBusinessValue()));
		
		Element estimatedTimeEl = new Element("estimatedTime");
		estimatedTimeEl.setText(String.valueOf(story.getEstimatedTime()));
		
		Element usedTimeEl = new Element("usedTime");
		usedTimeEl.setText(String.valueOf(story.getUsedTime()));
		
		Element riskEl = new Element("risk");
		riskEl.setText(String.valueOf(story.getRisk()));
		
		Element expEl = new Element("experimental");
		expEl.setText(String.valueOf(story.isExperimental()));
		
		Element taskListEl = new Element("tasks");
		ArrayList<Task> tasks = story.getTasks();
		for (Task task : tasks) {
			taskListEl.addContent(createTaskElement(task));
		}
		
		Element developerListEl = new Element("developers");
		ArrayList<Developer> developers = story.getDevelopers();
		for (Developer developer : developers) {
			developerListEl.addContent(createDeveloperEl(developer));
		}
		
		userStoryEl.addContent(idEl);
		userStoryEl.addContent(nameEl);
		userStoryEl.addContent(generatedDateEl);
		userStoryEl.addContent(finishedDateEl);
		userStoryEl.addContent(descrEl);
		userStoryEl.addContent(businessEl);
		userStoryEl.addContent(estimatedTimeEl);
		userStoryEl.addContent(usedTimeEl);
		userStoryEl.addContent(riskEl);
		userStoryEl.addContent(expEl);
		userStoryEl.addContent(taskListEl);
		userStoryEl.addContent(developerListEl);
		return userStoryEl;
	}

	/**
	 * Utility method to create the proper JDom Element structure out of the
	 * object passed as parameter
	 * @param developer
	 * @return
	 */
	private Element createDeveloperEl(Developer developer) {
		Element developerEl = new Element("deverloperID");
		developerEl.setText(developer.getId());
		return developerEl;
	}

	/**
	 * Utility method to create the proper JDom Element structure out of the
	 * object passed as parameter
	 * @param task
	 * @return
	 */
	private Element createTaskElement(Task task) {
		Element taskElement = new Element("task");
		Element idEl = new Element("id");
		idEl.setText(task.getId());
		Element descriptionEl = new Element("description");
		descriptionEl.setText(task.getDescription());
		Element completedEl = new Element("isCompleted");
		completedEl.setText(String.valueOf(task.isComplete()));
		
		taskElement.addContent(idEl);
		taskElement.addContent(descriptionEl);
		taskElement.addContent(completedEl);
		
		return taskElement;
	}
}
